package com.travelshare.business.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.travelshare.common.core.domain.TreeSelect;
import com.travelshare.common.core.domain.entity.TreeColumnModel;
import com.travelshare.common.utils.DateUtils;
import com.travelshare.common.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.travelshare.business.mapper.InfoColumnMapper;
import com.travelshare.business.domain.InfoColumn;
import com.travelshare.business.service.IInfoColumnService;

/**
 * 栏目信息Service业务层处理
 * 
 * @author travelshare
 * @date 2024-02-22
 */
@Service
public class InfoColumnServiceImpl implements IInfoColumnService 
{
    @Autowired
    private InfoColumnMapper infoColumnMapper;

    /**
     * 查询栏目信息
     * 
     * @param columnId 栏目信息主键
     * @return 栏目信息
     */
    @Override
    public InfoColumn selectInfoColumnByColumnId(Long columnId)
    {
        return infoColumnMapper.selectInfoColumnByColumnId(columnId);
    }

    /**
     * 查询栏目信息列表
     * 
     * @param infoColumn 栏目信息
     * @return 栏目信息
     */
    @Override
    public List<InfoColumn> selectInfoColumnList(InfoColumn infoColumn)
    {
        return infoColumnMapper.selectInfoColumnList(infoColumn);
    }

    /**
     * 新增栏目信息
     * 
     * @param infoColumn 栏目信息
     * @return 结果
     */
    @Override
    public int insertInfoColumn(InfoColumn infoColumn)
    {
        infoColumn.setCreateTime(DateUtils.getNowDate());
        infoColumn.setDelFlag("0");
        return infoColumnMapper.insertInfoColumn(infoColumn);
    }

    /**
     * 修改栏目信息
     * 
     * @param infoColumn 栏目信息
     * @return 结果
     */
    @Override
    public int updateInfoColumn(InfoColumn infoColumn)
    {
        infoColumn.setUpdateTime(DateUtils.getNowDate());
        return infoColumnMapper.updateInfoColumn(infoColumn);
    }

    /**
     * 批量删除栏目信息
     * 
     * @param columnIds 需要删除的栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoColumnByColumnIds(Long[] columnIds)
    {
        return infoColumnMapper.deleteInfoColumnByColumnIds(columnIds);
    }

    /**
     * 删除栏目信息信息
     * 
     * @param columnId 栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteInfoColumnByColumnId(Long columnId)
    {
        return infoColumnMapper.deleteInfoColumnByColumnId(columnId);
    }

	@Override
	public List<TreeSelect> selectColumnTreeList(TreeColumnModel treeColumnModel) {
		InfoColumn infoColumn = new InfoColumn();
		infoColumn.setIsOpen(treeColumnModel.getState());
		 List<InfoColumn> colums = infoColumnMapper.selectInfoColumnList(infoColumn);
		 List<TreeColumnModel> list = new ArrayList<>();
		 if(colums != null && !colums.isEmpty()) {
			 for(InfoColumn cc : colums) {
				 TreeColumnModel c = new TreeColumnModel(cc.getColumnId(), cc.getParentId(), cc.getColumnName(), cc.getState(), cc.getColumnIcon());
				 c.setIsPic(cc.getIsPic());
				 list.add(c);
			 }
		 }
		 return buildColumnTreeSelect(list);
	}

	private List<TreeSelect> buildColumnTreeSelect(List<TreeColumnModel> list) {
        List<TreeColumnModel> deptTrees = buildDeptTree(list);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

	private List<TreeColumnModel> buildDeptTree(List<TreeColumnModel> list) {
		List<TreeColumnModel> returnList = new ArrayList<TreeColumnModel>();
        List<Long> tempList = list.stream().map(TreeColumnModel::getColumnId).collect(Collectors.toList());
        for (TreeColumnModel dept : list)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(list, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = list;
        }
        return returnList;
	}

	private void recursionFn(List<TreeColumnModel> list, TreeColumnModel t) {
		// 得到子节点列表
        List<TreeColumnModel> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TreeColumnModel tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
	}

	private List<TreeColumnModel> getChildList(List<TreeColumnModel> list, TreeColumnModel t) {
		List<TreeColumnModel> tlist = new ArrayList<TreeColumnModel>();
        Iterator<TreeColumnModel> it = list.iterator();
        while (it.hasNext())
        {
        	TreeColumnModel n = (TreeColumnModel) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getColumnId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
	}

	private boolean hasChild(List<TreeColumnModel> list, TreeColumnModel t) {
		return getChildList(list, t).size() > 0;
	}
}
